package com.xinruke.hostels.central.manage.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xinruke.hostels.cache.extract.extractor.DictInfoCacheExtractor;
import com.xinruke.hostels.central.manage.interfaces.dto.*;
import com.xinruke.hostels.central.manage.mapper.XtGonggaoMapper;
import com.xinruke.hostels.common.cache.ManageUserCache;
import com.xinruke.hostels.common.constants.SysDicConstant;
import com.xinruke.hostels.common.mybatis.MyExample;
import com.xinruke.hostels.common.po.XtGonggao;
import com.xinruke.hostels.common.vo.ResultVO;
import com.xinruke.hostels.common.vo.query.EConditionOperator;
import com.xinruke.hostels.common.vo.query.QueryConditions;
import com.xinruke.hostels.common.vo.query.RowsDataVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author GuoZheng
 * @ClassName NoticeServiceImpl
 * @date：2019/6/16
 * @version: V1.0.0
 * @description：
 */
@Service
public class NoticeService {
    @Resource
    private XtGonggaoMapper xtGonggaoMapper;

    @Autowired
    private ManageUserCache manageUserCache;

    @Autowired
    private DictInfoCacheExtractor dictInfoCacheExtractor;

    public RowsDataVO<NoticeQueryResultDTO> getNoticeList(NoticeQueryDTO noticeQueryDTO) {
        RowsDataVO<NoticeQueryResultDTO> dataVO = new RowsDataVO();
        QueryConditions conditions = new QueryConditions();

        conditions.addCondition("biaoti", EConditionOperator.LIKE, noticeQueryDTO.getBiaoti());
        conditions.addCondition("leixing", EConditionOperator.LIKE, noticeQueryDTO.getLeixing());
        conditions.addCondition("zhuangtai", EConditionOperator.LIKE, noticeQueryDTO.getZhuangtai());

        MyExample example = new MyExample(XtGonggao.class, conditions, noticeQueryDTO.getOrderFields());
        Page<NoticeQueryResultDTO> page = PageHelper.startPage(noticeQueryDTO.getPageNo(), noticeQueryDTO.getPageSize());
        List<XtGonggao> xtGonggaoList = xtGonggaoMapper.selectByExample(example);

        List<NoticeQueryResultDTO> noticeQueryResultDTOList = xtGonggaoList.stream().map(n -> {
            NoticeQueryResultDTO dto = new NoticeQueryResultDTO();
            BeanUtils.copyProperties(n, dto);
            dto.setZhuangtaiMingCheng(dictInfoCacheExtractor.getDictValue(SysDicConstant.GONGGAOZHUANGTAI, dto.getZhuangtai()));
            dto.setLeixingMingCheng(dictInfoCacheExtractor.getDictValue(SysDicConstant.GONGGAOZHENGCELEIXING, dto.getLeixing()));
            dto.setChuangjianrenMingCheng(dto.getChuangjianrenId() != null ? dto.getChuangjianrenId().toString() : "");
            dto.setGengxinrenMingCheng(dto.getGengxinrenId() != null ? dto.getGengxinrenId().toString() : "");
            return dto;
        }).collect(Collectors.toList());

        dataVO.setPageNo(page.getPageNum());
        dataVO.setTotalRecords(page.getTotal());
        dataVO.setTotalPages(page.getPages());
        dataVO.setRows(noticeQueryResultDTOList);
        return dataVO;
    }


    public NoticeDetailQueryResultDTO getNoticeDetail(NoticeDetailQueryDTO noticeDetailQueryDTO) {

        XtGonggao xtGonggao = xtGonggaoMapper.selectByPrimaryKey(noticeDetailQueryDTO.getId());
        NoticeDetailQueryResultDTO dto = new NoticeDetailQueryResultDTO();
        BeanUtils.copyProperties(xtGonggao, dto);
        dto.setZhuangtaiMingCheng(dictInfoCacheExtractor.getDictValue("162021", dto.getZhuangtai()));
        dto.setLeixingMingCheng(dictInfoCacheExtractor.getDictValue("162022", dto.getLeixing()));

        return dto;
    }


    @Transactional
    public int addNotice(NoticeAddDTO noticeAddDTO) {
        XtGonggao xtGonggao = new XtGonggao();
        BeanUtils.copyProperties(noticeAddDTO, xtGonggao);
        xtGonggao.setChuangjianrenId(manageUserCache.getCurrentUserId());
        xtGonggao.setChuangjianshijian(new Date());
        xtGonggao.setGengxinrenId(manageUserCache.getCurrentUserId());
        xtGonggao.setGengxinshijian(new Date());

        xtGonggao.setZhuangtai(XtGonggao.ZHUANGTAI_XIUGAI);
        xtGonggao.setShanchubiaoshi(XtGonggao.SHANCHUBIAOSHI_WU_XIOA);
        return xtGonggaoMapper.insert(xtGonggao);
    }


    @Transactional
    public ResultVO modify(NoticeModifyDTO noticeModifyDTO) {
        ResultVO resultVO = ResultVO.fail();
        checkIsCanModify(noticeModifyDTO.getId(), resultVO);
        if (resultVO.getCode() == ResultVO.FAIL) {
            return resultVO;
        }
        XtGonggao xtGonggao = new XtGonggao();
        BeanUtils.copyProperties(noticeModifyDTO, xtGonggao);
        xtGonggao.setGengxinrenId(manageUserCache.getCurrentUserId());
        xtGonggao.setGengxinshijian(new Date());
        xtGonggao.setZhuangtai(XtGonggao.ZHUANGTAI_XIUGAI);
        xtGonggao.setShanchubiaoshi(XtGonggao.SHANCHUBIAOSHI_WU_XIOA);
        int i = xtGonggaoMapper.updateByPrimaryKeySelective(xtGonggao);
        if (i > 0) {
            resultVO.setCode(ResultVO.SUCCESS);
            resultVO.setMessage("操作成功");
        }
        return resultVO;

    }


    @Transactional
    public ResultVO release(NoticeReleseOrRevokeDTO noticeReleseDTO) {
        ResultVO resultVO = ResultVO.fail();
        checkIsCanRelease(noticeReleseDTO.getId(), resultVO);
        if (resultVO.getCode() == ResultVO.FAIL) {
            return resultVO;
        }
        int i = updateNoticeState(noticeReleseDTO.getId(), XtGonggao.ZHUANGTAI_YIFABU, XtGonggao.SHANCHUBIAOSHI_WU_XIOA);
        if (i > 0) {
            resultVO.setCode(ResultVO.SUCCESS);
            resultVO.setMessage("操作成功");
        }
        return resultVO;
    }


    @Transactional
    public ResultVO revoke(NoticeReleseOrRevokeDTO noticeReleseDTO) {
        ResultVO resultVO = ResultVO.fail();
        checkIsCanRevoke(noticeReleseDTO.getId(), resultVO);
        if (resultVO.getCode() == ResultVO.FAIL) {
            return resultVO;
        }
        int i = updateNoticeState(noticeReleseDTO.getId(), XtGonggao.ZHUANGTAI_CHEXIAO, XtGonggao.SHANCHUBIAOSHI_WU_XIOA);
        if (i > 0) {
            resultVO.setCode(ResultVO.SUCCESS);
            resultVO.setMessage("操作成功");
        }
        return resultVO;
    }


    @Transactional
    public ResultVO del(NoticeReleseOrRevokeDTO noticeReleseDTO) {
        ResultVO resultVO = ResultVO.fail();
        //检查是否可以删除
        checkIsCanDel(noticeReleseDTO.getId(), resultVO);

        if (resultVO.getCode() == ResultVO.FAIL) {
            return resultVO;
        }

        int i = updateNoticeState(noticeReleseDTO.getId(), XtGonggao.ZHUANGTAI_FEICHU, XtGonggao.SHANCHUBIAOSHI_YOU_XIAO);
        if (i > 0) {
            resultVO.setCode(ResultVO.SUCCESS);
            resultVO.setMessage("操作成功");
        }
        return resultVO;
    }


    private void checkIsCanDel(Integer id, ResultVO resultVO) {
        resultVO.setCode(ResultVO.SUCCESS);
        XtGonggao xtGonggao = xtGonggaoMapper.selectByPrimaryKey(id);
        if (xtGonggao == null) {
            resultVO.setCode(ResultVO.FAIL);
            resultVO.setMessage("该订单不存在");
        }
        if (xtGonggao.getZhuangtai().equals(XtGonggao.ZHUANGTAI_YIFABU)) {
            resultVO.setCode(ResultVO.FAIL);
            resultVO.setMessage("该订单已发布，需要撤销才能删除");
        }

    }


    private void checkIsCanRevoke(Integer id, ResultVO resultVO) {
        resultVO.setCode(ResultVO.SUCCESS);
        XtGonggao xtGonggao = xtGonggaoMapper.selectByPrimaryKey(id);
        if (xtGonggao == null) {
            resultVO.setCode(ResultVO.FAIL);
            resultVO.setMessage("该订单不存在");
        }
        if (!xtGonggao.getZhuangtai().equals(XtGonggao.ZHUANGTAI_YIFABU)) {
            resultVO.setCode(ResultVO.FAIL);
            resultVO.setMessage("该订单还没有发布");
        }
    }

    private void checkIsCanRelease(Integer id, ResultVO resultVO) {
        resultVO.setCode(ResultVO.SUCCESS);
        XtGonggao xtGonggao = xtGonggaoMapper.selectByPrimaryKey(id);
        if (xtGonggao == null) {
            resultVO.setCode(ResultVO.FAIL);
            resultVO.setMessage("该订单不存在");
        }
        if (xtGonggao.getZhuangtai().equals(XtGonggao.ZHUANGTAI_YIFABU)) {
            resultVO.setCode(ResultVO.FAIL);
            resultVO.setMessage("该订单已发布");
        }
        if (xtGonggao.getZhuangtai().equals(XtGonggao.ZHUANGTAI_FEICHU)) {
            resultVO.setCode(ResultVO.FAIL);
            resultVO.setMessage("该订单已废除");
        }
    }


    private void checkIsCanModify(Integer id, ResultVO resultVO) {
        resultVO.setCode(ResultVO.SUCCESS);
        XtGonggao xtGonggao = xtGonggaoMapper.selectByPrimaryKey(id);
        if (xtGonggao == null) {
            resultVO.setCode(ResultVO.FAIL);
            resultVO.setMessage("该订单不存在");
        }
        if (xtGonggao.getZhuangtai().equals(XtGonggao.ZHUANGTAI_YIFABU)) {
            resultVO.setCode(ResultVO.FAIL);
            resultVO.setMessage("该订单已发布,不能修改");
        }
    }

    /**
     * 更新订单状态
     *
     * @param id
     * @param zhuangtai
     * @param shanchubioashi
     * @return
     */
    private int updateNoticeState(Integer id, Byte zhuangtai, Integer shanchubioashi) {
        XtGonggao xtGonggao = new XtGonggao();
        xtGonggao.setId(id);
        xtGonggao.setGengxinrenId(manageUserCache.getCurrentUserId());
        xtGonggao.setGengxinshijian(new Date());
        xtGonggao.setZhuangtai(zhuangtai);
        xtGonggao.setShanchubiaoshi(shanchubioashi);
        return xtGonggaoMapper.updateByPrimaryKeySelective(xtGonggao);
    }

}
